<?php
require_once(dirname(__FILE__)."/../include/db_mysql.php");
require_once(dirname(__FILE__)."/../include/httpclient.php");

function insert_party_reg($email_account, $partyname, $password)
{
	$sql = new SqlClass;
	//连接数据库
	$sql->connect();
	//执行sql语句

	//先检验email账户是否已被注册
    $sql_command = "SELECT * FROM `user` where email='$email_account';";
    $r = $sql->exec_sql( $sql_command );
	$row = mysql_fetch_array($r);
	if( $row != '' )  //说明该email账户已被注册
	{
		$sql->close();
		return false;
	}

    //说明email账户未被注册
    $sql_command = "INSERT INTO  `user` (
					`email` ,
					`password`
					)
					VALUES (
					'$email_account',  '$password'
					);";
    $r = $sql->exec_sql( $sql_command );//保存执行sql的结果

	$sql_command = "SELECT userId FROM `user` where email='$email_account';";//得到刚才插入的那个userId。
	$r = $sql->exec_sql( $sql_command );//保存执行sql的结果
	$row = mysql_fetch_array($r);
	$userId='';
	if( $row != '' )  //说明该用户插入user表成功
	{
		$userId = $row[0];
	}
	else  //说明该用户插入user表失败。
	{
		$sql->close();
		return false;
	}

	//下面让party系统新建一个party记录，得到这个partyId，然后将userId和partyId，插入userparty表
	$partyId;
	$arg = array( 'partyname' => $partyname
		        );
	$arg['partyname'] = urlencode( $arg['partyname'] );
	$partyId = call_serviceurl( 35, $arg );//得到这个partyId

	if( $partyId == "serviceurl_unexpected_error")//serviceurl调用异常
	{
		$sql->close();
		return false;
	}

	if( $partyId == false )//说明新建partyId出错
	{
		$sql->close();
		return false;
	}

	//既然得到userId和partyId，那么就可以插入userParty表。
    $sql_command = "INSERT INTO  `user`.`userparty` (
					`userId` ,
					`partyId`
					)
					VALUES (
					'$userId',  '$partyId'
					);";
	$r = $sql->exec_sql( $sql_command );//保存执行sql的结果

	if( mysql_error() )//刚才插入失败
	{
		$sql->close();
		return false;
	}

	//userParty表插入成功。
	
	//下面让facility新建一个记录，得到新建的facilityId。然后将partyId和facilityId插入partyfacility表。
    $arg = array( 'act' => 'create_one_facility'
		        );
	$facilityId = call_serviceurl( 38, $arg );//得到这个facilityId

	if( $facilityId == '' ) //新建facility记录失败
	{
		$sql->close();
		return false;
	}
    
	//既然得到了partyId和facilityId，那么就可以插入partyfacility表
	$arg = array( 'act' => 'insert_partyfacility',
		          'partyid' => $partyId,
		          'facilityid' => $facilityId
		        );
	$r = call_serviceurl( 39, $arg );//插入partyfacility。

	if( !$r ) //插入partyfacility失败
	{
		$sql->close();
		return false;
	}

    //一路全都成功
	$sql->close();
	return true;
}
?>